home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1619 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.7 KB  |  84 lines

  1. Path: engnews2.Eng.Sun.COM!usenet
  2. From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Quick, easy question on externs...
  5. Date: 11 Jan 1996 20:30:59 GMT
  6. Organization: SunSoft
  7. Message-ID: <NITIN.96Jan11123059@more.eng.sun.com>
  8. References: <30F4333C.41A9@omni.voicenet.com> <30f46f5b.430017088@nntp.ix.netcom.com>
  9. NNTP-Posting-Host: more.eng.sun.com
  10. In-reply-to: miker3@ix.netcom.com's message of Thu, 11 Jan 1996 02:19:23 GMT
  11.  
  12. In article <30f46f5b.430017088@nntp.ix.netcom.com> miker3@ix.netcom.com (Mike Rubenstein) writes:
  13.  
  14. >   From: miker3@ix.netcom.com (Mike Rubenstein)
  15. >   Newsgroups: comp.lang.c++
  16. >   Date: Thu, 11 Jan 1996 02:19:23 GMT
  17. >   Organization: Netcom
  18. >   X-NETCOM-Date: Wed Jan 10  6:18:00 PM PST 1996
  19. >
  20. >   David Zuckman <dzuckman@omni.voicenet.com> wrote:
  21. >
  22. >   |>I got two source files:
  23. >   |>
  24. >   |>z1.cpp
  25. >   |>======
  26. >   |>    #include <iostream.h>
  27. >   |>
  28. >   |>    extern int const x;
  29. >   |>
  30. >   |>    void main( void ) {
  31. >   |>        x;
  32. >   |>    }
  33. >   |>
  34. >   |>z2.cpp
  35. >   |>======
  36. >   |>    int const x = 5;
  37. >   |>
  38. >   |>They compile and link fine (I'm using MS Visual C++ 2.1).
  39. >   |>
  40. >   |>I change the line in z1.cpp that reads
  41. >   |>        x;
  42. >   |>to
  43. >   |>        cout << x;
  44. >   |>and I get
  45. >   |>
  46. >   |>    Incrementally linking...
  47. >   |>    LINK : performing full link
  48. >   |>    z1.obj : error LNK2001: unresolved external symbol "?x@@3HB
  49. >   (int const  x)"
  50. >   |>    WinDebug/dbx.exe : error LNK1120: 1 unresolved externals
  51. >   |>
  52. >   |>What gives?
  53. >
  54. >   Looks like the optimizer allowed you to get away with incorrect code
  55. >   initially.  What probably happend is that the compiler realized that
  56. >   x; doesn't do anything useful and eliminated the statement so there
  57. >   was no reference to x in the object module for z1.cpp.
  58. >
  59. >   In C++, const implies static linkage so the x defined in z2.cpp is not
  60. >   available outside that file.  To fix this, change the definition in
  61. >   z2.cpp to
  62. >
  63. >       extern int const x = 5;
  64. >
  65. >
  66. >   Michael M Rubenstein
  67. >
  68.  
  69. Michael is right but even better way to do this is to put the extern
  70. definition in a header file and include it in both the files so that
  71. inconsistencies in the value of x is immediately noticed at compile
  72. time.  You still have to assign the value to x in z2.cpp.  Now if
  73. you change the value of x in one place and forgot to change it in
  74. the other, the compiler will immediately warn you.
  75.  
  76. -Nitin
  77.  
  78. -- 
  79. ----------------------------------------------------------------------
  80. Nitin More                                                         
  81. SunSoft, Bldg 16  Off: (415) 786 7109                                 
  82. Menlo Park, CA    Fax: (415) 786 7957   e-mail: nitin@more.eng.sun.com
  83. ----------------------------------------------------------------------
  84.